<script>
	import {
		doLogin,
		judeRegister
	} from '@/request/api.js'
	import {
		toast,
		useRouter,
		setStorageSync
	} from '@/request/utils.js'
	import store from '@/store/index.js'
	import {
		mapMutations
	} from 'vuex';
	export default {
		onLaunch: function() {
			console.log('App Launch')
			this.userLogin();
			const updateManager = uni.getUpdateManager()
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
				console.log(res.hasUpdate)
			})
			updateManager.onUpdateReady(function() {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success: function(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate()
						}
					}
				})
			})
			updateManager.onUpdateFailed(function() {
				// 新的版本下载失败
				uni.showModal({
					title: '更新提示',
					content: '新版本下载失败',
					showCancel: false
				})
			})
		},
		onShow: function() {
			console.log('App Show')
			// this.userLogin();
			
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			...mapMutations(['setLogin', 'setUserInfo', 'setToken']),
			userLogin() {
				uni.checkSession({
					success: () => {
						//存在登陆态
						// if (this.isLogin) {
						// 	return
						// }
						this.onLogin()
					},
					fail: () => {
						//不存在登陆态
						this.setLogin(false);
						this.setToken('');
						this.setUserInfo(null);
						this.onLogin()
					}
				})
			},
			onLogin() {
				uni.login({
					success: async (res) => {
						if (res.code) {
							//发起网络请求
							let data = await doLogin({
								code: res.code
							});
							let openid = data.data.openid;
							let unionid = data.data.unionid;
							let registerData = await judeRegister({
								userOpenId: openid,
								userUnionId: unionid
							})
							setStorageSync("oid", openid);
							setStorageSync("uid", unionid);
							if (registerData.code == 400) {
								// 没有注册
								this.goToNoLoginPage('/pages/login/login');
								this.setLogin(false);
							} else {
								// 以及注册。返回登录后的数据
								this.setUserInfo(registerData.data);
								this.setLogin(true);
								this.setToken(registerData.token);
								// 在此获取news页面总共的未读消息
								uni.setTabBarBadge({ //显示数字  
									index: 1, //tabbar下标
									text: '99' //数字
								})
								store.state.resolve()
							}
						}
					},
					fail: (res) => {}
				})
			}

		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	@import '@/static/customicons.css';

	// 设置整个项目的背景色
	page {
		background-color: #F2F2F2;
		color: #3D3D3D;
		line-height: 45rpx;
		height: 100%;
		font-size: 29rpx;
		font-weight: 400;
		font-family: PingFang SC, PingFang SC;
	}

	.ligth_page {
		background-color: #FFFFFF;
		min-height: 100%;

		page {
			background-color: #FFFFFF;
		}
	}

	.uni-forms-item__label {
		display: block;
		// width: auto !important;
		margin-right: 20rpx;
		white-space: nowrap;
		font-size: 32rpx !important;
		z-index: 1;
	}
	.uni-input-input {
		display: block;
		// width: auto !important;
		margin-right: 20rpx;
		font-size: 32rpx !important;
		z-index: 1;
	}

	.uni-forms-item {
		flex: 1;
		margin-bottom: 0 !important;
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
	}
	.uni-select__input-box {
		width: 100%;
	}
	.textarea {
		width: 100%;
		word-break: break-all;
	}
	.text-right {
		text-align: right;
	}
	

	image {
		display: block;
		width: 100%;
		height: auto;
	}

	button {
		display: block;
		width: 100%;
	}

	.b-bg {
		background-color: #456fee;
		color: #ffff;
	}

	.b-grey-bg {
		background-color: #e9e9e9;
		color: #ffff;
	}

	.btn-r {
		border-radius: 100px;

		&::after {
			border: none;
		}
	}

	.btn_init {
		background: none;
		border-radius: 0;
		border: 0;
		margin: 0;
		padding: 0;
		font-size: 30rpx;
		width: auto;

		&::after {
			border: none;
		}
	}

	.uni-tag {
		// word-break: break-all;
		max-width: 50vw;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	/* 单行文本溢出效果 */
	.no-wrap {
		display: -webkit-box;
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
	}

	/* 两行溢出 */
	.two-row {
		display: -webkit-box;
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	/* 三行溢出 */
	.three-row {
		display: -webkit-box;
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
	}
</style>